﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TravianHelper.app;

namespace TravianHelper.tools
{
    public partial class FindElephanForm : Form
    {
        public FindElephanForm()
        {
            InitializeComponent();
        }

        private void buttonFindElephan_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
            buttonFindElephan.Enabled = false;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int x = Convert.ToInt32(textBoxXElephan.Text);
            int y = Convert.ToInt32(textBoxYElephan.Text);
            for (int i = 0; i < TravianApplicationContext.allOasis.Count; i++)
            {
                Zone zone = (Zone)TravianApplicationContext.allOasis[i];
                zone.distance = Math.Sqrt((zone.x - x) * (zone.x - x) + (zone.y - y) * (zone.y - y));
            }
            TravianApplicationContext.allOasis.Sort(new ZoneComparer());
            listViewTarget.Items.Clear();
            for (int i = 0; i < TravianApplicationContext.allOasis.Count; i++)
            {
                Zone zone = (Zone)TravianApplicationContext.allOasis[i];
                Console.WriteLine(i + "/" + TravianApplicationContext.allOasis.Count);
                if (TravianApplicationContext.adapter.has(zone.x, zone.y, "Elephan"))
                {
                    AddListView(new string[] { zone.x.ToString() + "," + zone.y.ToString(), zone.distance.ToString(), zone.status, zone.defend.ToString() });
                }
                backgroundWorker1.ReportProgress(i + 1);
            }
        }

        private void AddListView(string[] items)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.listViewTarget.InvokeRequired)
            {
                AddListViewCallback d = new AddListViewCallback(AddListView);
                this.Invoke(d, new object[] { items });
            }
            else
            {
                listViewTarget.Items.Add(new ListViewItem(items));
            }
        }

        delegate void AddListViewCallback(string[] items);

       

    }
}
